package com.imooc.controller;

import com.imooc.base.BaseInfoProperties;
import com.imooc.entity.bo.CommentBo;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.service.CommentService;
import com.imooc.service.MsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController extends BaseInfoProperties {
    @Autowired
    private CommentService commentService;

    @PostMapping("/create")
    public GraceJSONResult createComment(@Valid @RequestBody CommentBo commentBo){
        return GraceJSONResult.ok(commentService.createComment(commentBo));
    }
    @GetMapping("/list")
    public GraceJSONResult getCommentList(@RequestParam("vlogId")String vlogId,
                                          @RequestParam("userId")String userId,
                                          @RequestParam("page")Integer page,
                                          @RequestParam("pageSize")Integer pageSize){
        if(page==null){
            page = COMMON_START_PAGE;
        }
        if(pageSize==null){
            pageSize = COMMON_PAGE_SIZE;
        }
        return GraceJSONResult.ok(commentService.getCommentList(vlogId,userId,page,pageSize));
    }
    @GetMapping("/counts")
    public GraceJSONResult getCommentCounts(@RequestParam("vlogId")String vlogId){
        String countStr = redis.get(REDIS_USER_COMMENT_COUNTS + ":" + vlogId);
        if(countStr==null){
            countStr = "0";
        }
        return GraceJSONResult.ok(Integer.valueOf(countStr));
    }
    @PostMapping("/like")
    public GraceJSONResult userBeLikedComment(@RequestParam("userId")String userId,
                                              @RequestParam("commentId")String commentId){
        commentService.userBeLikedComment(userId,commentId);
        return GraceJSONResult.ok();
    }
    @PostMapping("/unlike")
    public GraceJSONResult userBeUnLikedComment(@RequestParam("userId")String userId,
                                              @RequestParam("commentId")String commentId){
        commentService.userBeUnLikedComment(userId,commentId);
        return GraceJSONResult.ok();
    }
    @DeleteMapping("/delete")
    public GraceJSONResult userDeleteComment(@RequestParam("commentUserId")String commentUserId,
                                             @RequestParam("commentId")String commentId,
                                             @RequestParam("vlogId")String vlogId){
        commentService.delThisCount(commentUserId,commentId,vlogId);
        return GraceJSONResult.ok();
    }
}
